package controller.admin;

import java.util.*;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.validator.ValidatorException;
import model.*;
import helper.*;

@ManagedBean(name = "acmb")
@SessionScoped
public class AccountManagedBean {
    @EJB
    private EmployeeFacade employeeFacade;

    @EJB
    private AccountFacade accountFacade;
    
    Account ac = new Account();
    Account ac1 = new Account();

    public void setEmployeeFacade(EmployeeFacade employeeFacade) {
        this.employeeFacade = employeeFacade;
    }

    public void setAc1(Account ac1) {
        this.ac1 = ac1;
    }
    private boolean islogin;
    private String username;
    private String password;
    private String EmployeeID;
    private String message = new String();

    public EmployeeFacade getEmployeeFacade() {
        return employeeFacade;
    }

    public AccountFacade getAccountFacade() {
        return accountFacade;
    }

    public Account getAc1() {
        return ac1;
    }

    public String getMessage() {
        return message;
    }

    public String getEmployeeID() {
        return EmployeeID;
    }

    public void setEmployeeID(String EmployeeID) {
        this.EmployeeID = EmployeeID;
    }
    
    

    private Employee em = new Employee();

    public Employee getEm() {
        return em;
    }

    public void setEm(Employee em) {
        this.em = em;
    }
    public boolean isIslogin() {
        return islogin;
    }

    public void setIslogin(boolean islogin) {
        this.islogin = islogin;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAccountFacade(AccountFacade accountFacade) {
        this.accountFacade = accountFacade;
    }

    public Account getAc() {
        return ac;
    }

    public void setAc(Account ac) {
        this.ac = ac;
    }

    public AccountManagedBean() {
    }

    public List<Account> accountList() {
        return this.accountFacade.findAll();
    }
    public String addAccount() {
       String page ="Create";
        try {
            this.ac.setPassword(PasswordEncryption.MD5(this.ac.getPassword()));
            this.ac.setEmployeeID(this.employeeFacade.find(this.EmployeeID));
            this.accountFacade.create(ac); 
             page ="List";
        } catch (Exception e) {
            
           
            
        }
       
    
        return  page;
        
    }

    public String editAccount() {
        String page = "";
        try {
             this.ac.setPassword(PasswordEncryption.MD5(this.ac.getPassword()));
             this.ac.setEmployeeID(this.employeeFacade.find(this.EmployeeID));
            this.accountFacade.edit(ac);
            page ="List";
        } catch (Exception e) {
        }
       
        return page;
    }

    public String deleteAccount(Account acc) {
        String page ="";
        try {
             this.accountFacade.remove(acc);
        page = "List";
        } catch (Exception e) {
            this.message = "Delete not found";
        }
       return page;
    }

    public String acEdit(Account acc) {
        this.ac = acc;
        return "Edit";
    }

    public String acView(Account acc) {
        this.ac = acc;
        return "View";
    }

    public void login() {
       try{
           this.ac = this.accountFacade.Login(this.username, PasswordEncryption.MD5(this.password));
           this.islogin = true;
                if (this.ac.getRole() == 1) {
                    this.ac1 = this.ac;
                    doredirect("account/List.xhtml");
                }
                if (this.ac.getRole() == 2) {
                    this.ac1 = this.ac;
                    doredirect("hrGroup/Search.xhtml");
                }
                if (this.ac.getRole() == 3) {
                    this.ac1 = this.ac;
                    doredirect("interviewer/ListInterview.xhtml");
                }
       }catch(Exception e){
           System.out.println(e.getMessage());
       }
                

            
        
    }

    public void logout() {
        this.islogin = false;
        doredirect("../login.xhtml");
    }

    public void verifylogin() {
        if (!this.islogin) {
            doredirect("../login.xhtml");
        }
    }

    public void doredirect(String url) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(url);
        } catch (Exception e) {
        }
    }
}